<template>
  <div>

    <nav-btn :sheet-header="xlsxHeader"
             :sheet-list="_list"
             xlsx-name="志愿者列表"
             @search-method="handleGetSearchActivity"
    />


    <el-table :data="list" border max-height="680"
              style="width: 100%">


      <el-table-column
          label="序号"
          type="index"
      />
      <el-table-column
          v-for="(item ,index ) in settings"
          :label="item"
          :key="item"
          :prop="index"
          v-if="index!=='teacherId'"
      />
      <el-table-column label="操作"
                       width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handleDelTeacher(scope.row.teacherId)">移除教师</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        :current-page="currentPage"
        :page-size="currentPageSize"
        :page-sizes="[10,50,100,500,1000,99999]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleGetBySizeActivity"
        @current-change="handleGetByNumActivity"/>
    <el-button @click="addContentTeacherShow = true"
               type="primary"
               style="width: 100%"
               plain
    >添加老师
    </el-button>
    <el-dialog :close-on-click-modal="false"
               :close-on-press-escape="false"
               :visible.sync="addContentTeacherShow" title="添加教师">
      <add-teacher-pannel @cancel="handleCancel"/>
    </el-dialog>
  </div>
</template>

<script>

import NavBtn from "@/components/navBtn";

export default {
  data () {
    return {
      addContentTeacherShow: false,
      changeTeacherShow: false,
      list: [],
      currentPage: 1,
      currentPageSize: 10,
      total: 0,
      form: {
        courseName: "",
        courseDisc: "",
        courseArrangePrice: "",
        coursePeople: "",
        courseImage: "",
        managerId: "",
      },
      changeForm: {
        courseName: "",
        courseDisc: "",
        courseArrangePrice: "",
        coursePeople: "",
        courseImage: "",
        managerId: "",
      },
      settings: {
        "teacherId": "教师ID",
        "teacherName": "教师姓名",
        "teacherIdentify": "身份证号码",
        "teacherAccount": "教师账号",
        "teacherAge": "教师年龄",
        "teacherSex": "教师性别",
        "teacherDisc": "教师简介",
        "teacherTitle": "教师职称",
      },
    };
  },
  mounted () {
    this.handleGetTeacher();
  },
  computed: {
    xlsxSheet () {
      const list = this.list;
      let _list = [];
      _list = list.map(value => {
        console.log(`value-->`, value);
        return {
          "teacherId": value?.teacherId,
          "teacherName": value?.teacherName,
          "teacherIdentify": value?.teacherIdentify,
          "teacherAccount": value?.teacherAccount,
          "teacherAge": value?.teacherAge,
          "teacherSex": value?.teacherSex,
          "teacherDisc": value?.teacherDisc,
          "teacherTitle": value?.teacherTitle,
        };
      });

      return _list;
    },
    xlsxHeader () {
      const { settings } = this;
      return Object.values(settings);
    },
    _list () {
      const { xlsxSheet } = this;
      let _list = xlsxSheet.map(value => {
        return [Object.values(value)];
      });
      return _list.flat(1);
    },
  },

  methods: {
    async handleGetBySizeActivity (size) {
      let pageSize = this.currentPageSize;
      pageSize = size;
      await this.handleGetTeacher();
    },

    // 分页查询全部活动
    async handleGetByNumActivity (num) {
      let pageNum = this.currentPage;
      pageNum = num;
      await this.handleGetTeacher();
    },
    // 分页查询全部教师
    async handleGetTeacher () {
      let pageNum = this.currentPage;
      let pageSize = this.currentPageSize;
      const res = await this.$http.teacherAll({
        pageNum,
        pageSize,
      });
      if (res.code !== 0) return null;
      this.list = res.data.list;
      this.total = res.data.totalCount;
    },

    async handleChangeInfo (teacher) {
      // console.log(teacher)
      const res = this.$http.teacherModify(teacher);
    },
    async handleGetCourse (e) {
      // console.log(e)
      const res = await this.$http.managerGetCourse({ teacherId: e });
    },
    handleCancel () {
      console.log(this);
      this.addContentTeacherShow = false;
    },
    async handleDelTeacher (teacherId) {
      const res = await this.$http.managerDeleteTeacher({ teacherId });
      if (res.code !== 0) return null;
      return this.$router.go(0);
    },

    handleGetSearchActivity (info) {
      let {
        content,
        dataRange,
      } = info;

      return this.searchByBoth(content, dataRange);

    },
    async searchByBoth (content, dataRange) {
      //  后端接口

      const res = await this.$http.teacherGetAllBySearch(
          {
            from: dataRange ? dataRange[0] ?? "" : "",
            keywords: content ?? "",
            pageNum: this.currentPage ?? "",
            pageSize: this.currentPageSize ?? "",
            to: dataRange ? dataRange[1] ?? "" : "",
          },
      );
      if (res.code !== 0) return null;
      this.total = res.data.total;
      this.list = res.data.list;

    },
  },
  components: {
    NavBtn,
    "add-teacher-pannel": _ => import("./AddTeacher"),
  },
};
</script>

<style lang="less" scoped>
.commodity-img {
  width: 40px;
}

.el-table {
  margin-top: 20px;
}
</style>
